﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;

namespace HealthCommunity
{
	public class MyModule : IHttpModule
	{
		public void Init(HttpApplication app)
		{
			app.BeginRequest += new EventHandler(app_BeginRequest);

		}
		void app_BeginRequest(object sender, EventArgs e)
		{
			HttpContext context = ((HttpApplication)sender).Context;

			if (context.Request.ContentLength > 4096000)
			{
				IServiceProvider provider = (IServiceProvider)context;
				HttpWorkerRequest wr = (HttpWorkerRequest)provider.GetService(typeof(HttpWorkerRequest));
				FileStream fs = null;
				// Check if body contains data
				if (wr.HasEntityBody())
				{
					// get the total body length
					int requestLength = wr.GetTotalEntityBodyLength();
					// Get the initial bytes loaded
					int initialBytes = wr.GetPreloadedEntityBody().Length;

					if (!wr.IsEntireEntityBodyIsPreloaded())
					{
						byte[] buffer = new byte[512000];
						string[] fileName = context.Request.QueryString["fileName"].Split(new char[] { '\\' });
						fs = new FileStream(context.Server.MapPath("~/Uploads/" + fileName[fileName.Length - 1]), FileMode.CreateNew);
						// Set the received bytes to initial bytes before start reading
						int receivedBytes = initialBytes;
						while (requestLength - receivedBytes >= initialBytes)
						{
							// Read another set of bytes
							initialBytes = wr.ReadEntityBody(buffer, buffer.Length);
							// Write the chunks to the physical file
							fs.Write(buffer, 0, buffer.Length);
							// Update the received bytes
							receivedBytes += initialBytes;
						}
						initialBytes = wr.ReadEntityBody(buffer, requestLength - receivedBytes);

					}
				}
				fs.Flush();
				fs.Close();
				context.Response.Redirect("UploadFinished.aspx");
			}

		}
		public void Dispose()
		{
		}
	}
}